﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Contour.Addons.CourierSupport.ItemProviders.Entities;
using Umbraco.Courier.Persistence.V4.NHibernate;
using NHibernate.Criterion;

namespace Contour.Addons.CourierSupport.Helpers
{
    public class FormHelper
    {
        public static EntityClasses.Ufform Create(Form form)
        {
            var f = new EntityClasses.Ufform();
            f = Update(f, form);
            return f;
        }

        public static EntityClasses.Ufform Get(string guid)
        {
            return Get(new Guid(guid));
        }
        public static EntityClasses.Ufform Get(Guid guid)
        {
            var form = NHibernateProvider.GetCurrentSession().CreateCriteria<EntityClasses.Ufform>()
                       .Add(Restrictions.Eq("id", guid))
                       .List<EntityClasses.Ufform>().FirstOrDefault();

            return form;
        }

        public static EntityClasses.Ufform Update(EntityClasses.Ufform f, Form form)
        {
            f.Id = form.Id;
            f.Name = form.Name;
            f.Created = form.Created;
            f.Indicator = form.Indicator;
            f.ShowValidationSummary = form.ShowValidationSummary;
            f.HideFieldValidation = form.HideFieldValidation;
            f.RequiredErrorMessage = form.RequiredErrorMessage;
            f.InvalidErrorMessage = form.InvalidErrorMessage;
            f.MessageOnSubmit = form.MessageOnSubmit;
            f.GotoPageOnSubmit = form.GoToPageOnSubmit;
           
            
            f.ManualApproval = form.ManualApproval;
            f.Archived = form.Archived;
            f.StoreRecordsLocally = form.StoreRecordsLocally;
            f.DisableDefaultStylesheet = form.DisableDefaultStylesheet;

            //datasource id

            //xpath on submit comes from settings

            return f;
        }

        public static List<EntityClasses.Ufpage> GetPages(Guid formGuid)
        {
            return Get(formGuid).Ufpages.ToList();

        }

        public static List<EntityClasses.Uffieldset> GetFieldSets(Guid pageGuid)
        {
            return NHibernateProvider.GetCurrentSession().CreateCriteria<EntityClasses.Ufpage>()
                       .Add(Restrictions.Eq("id", pageGuid))
                       .List<EntityClasses.Ufpage>().FirstOrDefault().Uffieldsets.ToList();
        }

        public static List<EntityClasses.Uffield> GetFields(Guid fieldsetGuid)
        {
            return NHibernateProvider.GetCurrentSession().CreateCriteria<EntityClasses.Uffieldset>()
                      .Add(Restrictions.Eq("id", fieldsetGuid))
                      .List<EntityClasses.Uffieldset>().FirstOrDefault().Uffields.ToList();
        }

        public static List<EntityClasses.Ufprevalue> GetPrevalues(Guid fieldGuid)
        {
            return NHibernateProvider.GetCurrentSession().CreateCriteria<EntityClasses.Uffield>()
                       .Add(Restrictions.Eq("id", fieldGuid))
                       .List<EntityClasses.Uffield>().FirstOrDefault().Ufprevalues.ToList();
        }

    }
}